<?php
namespace Admin\Logic;

use Think\Log;

class MemberLogic
{
    /**
     * 检测用户名是否存在
     * @param  [type] $userName [用户名]
     * @return [type]           [description]
     */
    public static function verifyUserName($userName)
    {
        try {
            $field = ['id', 'user_name', 'pass_word', 'is_lock'];
            $data = D('user')->where(['user_name' => $userName])->fetchSql(false)->field($field)->find();
            return $data;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 获取管理员用户列表
     * @param array $where
     * @return mixed
     */
    public static function getAll(array $where = [], $field = '')
    {
        try {
            $user = D('Member');
            if (empty($where)) {
                $data = $user->order('created_at desc')->field($field)->select();
            } else {
                $where = isset($where['id'])?['id'=>$where['id']]:['nickname'=>['like','%'.$where['nickname'].'%']];
                $data = $user->where($where)->order('created_at desc')->field($field)->select();
            }

            return $data;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 执行添加评论操作
     * @param $param
     * @return array|bool
     */
    public static function create($param)
    {
        try {
            $user = M('member');
            $id = $user->add($param);
            if($id){
                return ['code' => 0, 'msg' => $id];
            } else {
                return ['code' => 1, 'msg' => '服务器找不到了'];
            }

        } catch (\Exception $e) {

            return false;
        }
    }

    /**
     * 执行添加评论操作
     * @param $param
     * @return array|bool
     */
    public static function createMessage($param)
    {
        try {
            $user = M('message');
            $order_id = $user->where(['order_id' => $param['order_id']])->find();
        
            if (!empty($order_id)) {
                $user->where(['order_id' => $param['order_id']])->save($param);
                return ['code' => 0, 'msg' => '数据保存成功'];
            }
            $id = $user->add($param);

            if($id){
                return ['code' => 0, 'msg' => $id];
            } else {
                return ['code' => 1, 'msg' => '服务器找不到了'];
            }

        } catch (\Exception $e) {

            return false;
        }
    }

    /**
     * 删除单条数据
     * @param $id
     * @return mixed
     */
    public static function delete($id)
    {
        try {
            $data = D('Member')->where(['id'=>$id])->delete();
            M('message')->where(['user_id'=>$id])->delete();
            return $data;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 批量删除多条数据
     * @param string $ids
     * @return mixed
     */
    public static function deleteAll($ids)
    {
        try {
            $data = D('User')->where(['id' => ['in', $ids]])->delete($ids);
            return $data;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 更新数据信息操作
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function update($data)
    {

        try {
            $update = D('Member')->save($data);
            return $update;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 根据员工id获取订单数
     * @param $member_id
     * @return bool|mixed
     */
    public static function getOrderNumber($member_id)
    {
        try{
            $number = D('Order')
                ->where(['person_id'=>$member_id])
                ->field(['sum(*)'])
                ->find();

            return $number;

        } catch (\Exception $e){
            return false;
        }

    }


    /**
     * @param $id
     * @return array
     */
    public static function getAllHistory($id)
    {
        $message =  M('message');

        $count = $message
            ->where(['user_id'=>$id])
            ->group('degree')
            ->field('count(degree) as 数量')
            ->select();

        $data = $message
            ->where(['user_id'=>$id])
            ->order('id desc')
            ->select();
        $data['总数'] = count($data);


        return ['data'=>$data,'count'=>$count];

    }


    public static function  getMemberLoss($member_id)
    {
        // 获取所有订单
        $order = D('Order as o')
            ->where(['person_id'=>$member_id])
            ->field(['id'])
            ->select();
        $order_id = [];
        foreach ($order as $k=>$v){
            $order_id[] = $v['id'];
        }
        if(empty($order_id)){
            return 0;
        }

        // 根据订单id查询菜谱信息
        $foods = OrderLogic::getFoodByOrderIds($order_id);
        $stock_loss = 0;
        foreach ($foods as $k=>$v)
        {
            switch ($v['spec']){
                case '大':
                    $foods[$k]['loss'] = json_decode($v['loss'], true)['大'];
                    break;

                case '中':
                    $foods[$k]['loss'] = json_decode($v['loss'], true)['中'];
                    break;

                case '小':
                    $foods[$k]['loss'] = json_decode($v['loss'], true)['小'];
                    break;
            }
            // 获取原料消耗
            $foods[$k]['stock_loss'] = FoodLogic::getStockLoss($v['food_id']);
            $stock_loss += $foods[$k]['stock_loss'];
        }


        // 计算出所有菜谱的损耗
        $loss_total = 0;

        foreach ($foods as $k=>$v)
        {
            $loss_total += $v['loss'];
        }


        // 素有订单的菜谱的原料损耗 + 损耗
        return $loss_total+$stock_loss;


    }
}